﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Workflow.Flow;
using System.Xml.Linq;
using Frame.Lib.Utilities;

namespace Jzy.Web.WorkFlow.Design
{
    public partial class BasicSet : System.Web.UI.Page
    {
        private string FlowID
        {
            get
            {
                return Request["FlowID"];
            }
        }

        private string ControlName
        {
            get
            {
                return Request["Name"];
            }
        }

        private FlowBO _flow = new FlowBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Require.Items.Add(new ListItem("是", "1"));
                Require.Items.Add(new ListItem("否", "0"));
                Require.SelectedValue = "0";
                DispInfo();
            }
        }

        private void DispInfo()
        {            
            FlowInfo info = _flow.GetInfo(FlowID);
            XElement root = XElement.Parse(info.FormTemplate);
            XElement control = root.Element(ElementItem.DataFields).Element(ControlName);

            if (control.Element(BasicSetItem.MaxLength) != null)//只要有一个存在，那就都会有数据
            {
                MaxLenth.Text = control.Element(BasicSetItem.MaxLength).Value;
                Prompt.Text = control.Element(BasicSetItem.Prompt).Value;
                DefaultValue.Text = control.Element(BasicSetItem.DefaultValue).Value;
                OnClick.Text = control.Element(BasicSetItem.OnClick).Value;
                Regular.Text = control.Element(BasicSetItem.Regular).Value;
                ErrorMessage.Text = control.Element(BasicSetItem.ErrorMessage).Value;
                Require.SelectedValue = control.Element(BasicSetItem.Require).Value;
            }


            txtControlID.Text = ControlName;
            txtControlName.Text = ControlName;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            FlowInfo info = _flow.GetInfo(FlowID);
            XElement root = XElement.Parse(info.FormTemplate);
            XElement control = root.Element(ElementItem.DataFields).Element(ControlName);


            SetValue(control, BasicSetItem.MaxLength, MaxLenth.Text);
            SetValue(control, BasicSetItem.Prompt, Prompt.Text);
            SetValue(control, BasicSetItem.DefaultValue, DefaultValue.Text);
            SetValue(control, BasicSetItem.OnClick, OnClick.Text);
            SetValue(control, BasicSetItem.Regular, Regular.Text);
            SetValue(control, BasicSetItem.ErrorMessage, ErrorMessage.Text);
            SetValue(control, BasicSetItem.Require, Require.SelectedValue);

            info.FormTemplate = root.ToString();

            _flow.Save(info);

            JSUtil.CloseDialog("保存成功");

        }

        private void SetValue(XElement control, string field, string value)
        {
            XElement content = control.Element(field);
            if (content == null)
            {
                content = new XElement(field);
                control.Add(content);
            }

            content.Value = value;
        }

    }
}
